<template>
  <el-container>
    <el-main>
      <el-row>
        <el-col :span="22" :offset="1">
          <el-button size="mini"  @click="opendialog">添加任务</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" :offset="1">
          <el-table :data="sshtasks"  border size="mini">
            <el-table-column prop="host" label="主机地址"></el-table-column>
            <el-table-column prop="dbhost" label="数据库地址"></el-table-column>
            <el-table-column prop="dbname" label="数据库名"></el-table-column>
            <el-table-column prop="crontab" label="计划任务"></el-table-column>
            <el-table-column prop="savepath" label="保存地址"></el-table-column>
            <el-table-column prop="created" label="创建时间">
              <template slot-scope="scope">
                <span>{{$public.comTime(scope.row.created)}}</span>
              </template>
            </el-table-column>
            <el-table-column prop label="操作">
              <template slot-scope="scope">
                <el-button type="text" @click="runJob(scope.row.id)">立即执行</el-button>
                <el-button type="text" @click="openedit(scope.row.id)">修改</el-button>
                <el-button type="text" @click="deleteTask(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <el-dialog
        :title="AddTaskTitle ? '修改任务':'添加任务'"
        size="mini"
        :visible.sync="dialogFormVisible"
        v-if="dialogFormVisible">
        <addsshtask @close="addclose" :task-id="taskId" :dialogtitle="AddTaskTitle"></addsshtask>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
import addsshtask from "@/components/addsshtask.vue";
export default {
  components: {
    addsshtask,
  },
  data() {
    return {
      sshtasks: [],
      taskId:"",
      AddTaskTitle: false,
      dialogFormVisible: false,
    };
  },
  created() {
    this.getsshtasks();
  },
  methods: {
    opendialog() {
      this.$router.push({
        path:"addsshtask",
        meta: { title: '添加任务1'}
      })

      // this.AddTaskTitle=false
      // this.taskId=""
      // this.dialogFormVisible = true;
    },
    getsshtasks() {
      this.$http.Get(this.$api.sshtasks, {}, (res) => {
        this.sshtasks = res.data;
      });
    },
    addclose() {
      this.dialogFormVisible = false;
      this.getsshtasks();
    },
    runJob(e) {
      this.$http.Put(this.$api.runsshjob+"/"+e,{},(res)=>{
        console.log(res)
      })
    },
    openedit(e) {
      this.$router.push({
        path:"addsshtask",
        query:{
          id: e
        },
        meta: { title: '修改任务'}
      })
    },
    deleteTask(e) {
      this.$http.Delete(this.$api.sshtask + "/" + e, {}, () => {
        this.getsshtasks();
      });
    },
  },
};
</script>